package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.time.Year;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UserTypeConstants;
import com.ruoyi.mentor.domain.*;
import com.ruoyi.mentor.service.impl.*;
import com.ruoyi.mentor.vo.ImportInstructorVo;
import com.ruoyi.mentor.vo.ImportStudentVo;
import com.ruoyi.mentor.vo.InstructorDetailVo;
import org.apache.xmlbeans.impl.xb.xmlconfig.Usertypeconfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper roleMapper;


    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SubjectServiceImpl subjectService;

    @Autowired
    StudentServiceImpl studentService;

    @Autowired
    private StudentDetailServiceImpl studentDetailService;

    @Autowired
    private StuMemberInfoServiceImpl memberInfoService;

    @Autowired
    private InstructorServiceImpl instructorService;


    @Autowired
    private InsDetailServiceImpl insDetailService;

    @Autowired
    private StuMemberInfoServiceImpl stuMemberInfoService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private UpdatePasswordServiceImpl updatePasswordService;

    @Autowired
    private InsPaperServiceImpl insPaperService;

    @Autowired
    private InsProjectServiceImpl insProjectService;

    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<SysUser> selectUserList(SysUser user) {
        return userMapper.selectUserList(user);
    }

    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserByUserName(String userName) {
        return userMapper.selectUserByUserName(userName);
    }

    /**
     * 通过用户ID查询用户
     *
     * @param userId 用户ID
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserById(Long userId) {
        return userMapper.selectUserById(userId);
    }

    /**
     * 查询用户所属角色组
     *
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public String selectUserRoleGroup(String userName) {
        List<SysRole> list = roleMapper.selectRolesByUserName(userName);
        StringBuffer idsStr = new StringBuffer();
        for (SysRole role : list) {
            idsStr.append(role.getRoleName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString())) {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }


    /**
     * 校验用户名称是否唯一
     *
     * @param userName 用户名称
     * @return 结果
     */
    @Override
    public String checkUserNameUnique(String userName) {
        int count = userMapper.checkUserNameUnique(userName);
        if (count > 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public String checkPhoneUnique(SysUser user) {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = userMapper.checkPhoneUnique(user.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public String checkEmailUnique(SysUser user) {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUser info = userMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验用户是否允许操作
     *
     * @param user 用户信息
     */
    @Override
    public void checkUserAllowed(SysUser user) {
        if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) {
            throw new CustomException("不允许操作超级管理员用户");
        }
    }

    /**
     * 新增保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUser(SysUser user) {
        // 新增用户信息
        int rows = userMapper.insertUser(user);
        // 新增用户与角色管理
        insertUserRole(user);
        return rows;
    }

    /**
     * 修改保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUser(SysUser user) {
        Long userId = user.getUserId();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        return userMapper.updateUser(user);
    }

    /**
     * 修改用户状态
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUserStatus(SysUser user) {
        return userMapper.updateUser(user);
    }

    /**
     * 修改用户基本信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUserProfile(SysUser user) {
        return userMapper.updateUser(user);
    }

    /**
     * 修改用户头像
     *
     * @param userName 用户名
     * @param avatar   头像地址
     * @return 结果
     */
    @Override
    public boolean updateUserAvatar(String userName, String avatar) {
        return userMapper.updateUserAvatar(userName, avatar) > 0;
    }

    /**
     * 重置用户密码
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int resetPwd(SysUser user) {
        return userMapper.updateUser(user);
    }

    /**
     * 重置用户密码
     *
     * @param userName 用户名
     * @param password 密码
     * @return 结果
     */
    @Override
    public int resetUserPwd(String userName, String password) {
        return userMapper.resetUserPwd(userName, password);
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUser user) {
        Long[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roles) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0) {
                userRoleMapper.batchUserRole(list);
            }
        }
    }


    /**
     * 通过用户ID删除用户
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserById(Long userId) {
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);

        SysUser sysUser = selectUserById(userId);
        Integer userType = sysUser.getUserType();

        //删除用户关联的学生或者导师
        if (UserTypeConstants.ROLE_STUDENT==userType){
            //删除学生信息
            studentService.removeStudentByUserId(userId);
        }else if (UserTypeConstants.ROLE_INSTRUCTOR==userType){
            instructorService.removeInstructorByUserId(userId);
        }

        return userMapper.deleteUserById(userId);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserByIds(Long[] userIds) {
        for (Long userId : userIds) {
            checkUserAllowed(new SysUser(userId));
        }
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);

        for (Long userId : userIds) {
            SysUser sysUser = selectUserById(userId);
            Integer userType = sysUser.getUserType();
            if (UserTypeConstants.ROLE_STUDENT==userType){
                //删除学生信息
                studentService.removeStudentByUserId(userId);
            }else if (UserTypeConstants.ROLE_INSTRUCTOR==userType){
                instructorService.removeInstructorByUserId(userId);
            }else {
                continue;
            }
        }

        return userMapper.deleteUserByIds(userIds);
    }

    /**
     * 导入用户数据
     *
     * @param userList        用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = configService.selectConfigByKey("sys.user.initPassword");

        List<SysUserRole> userRoleList = new ArrayList<>();

        SysRole sRole = roleMapper.checkRoleKeyUnique("student");
        SysRole iRole = roleMapper.checkRoleKeyUnique("instructor");
        for (SysUser user : userList) {
            try {
                // 验证是否存在这个用户
                SysUser u = userMapper.selectUserByUserName(user.getUserName());
                if (StringUtils.isNull(u)) {
                    user.setPassword(SecurityUtils.encryptPassword(password));
                    user.setCreateBy(operName);
                    user.setUserType(UserTypeConstants.ROLE_ADMIN);
                    this.insertUser(user);

                    SysUserRole sysUserRole = new SysUserRole();
                    if (user.getUserType() == UserTypeConstants.ROLE_STUDENT) {
                        sysUserRole.setRoleId(sRole.getRoleId());

                    } else {
                        sysUserRole.setRoleId(iRole.getRoleId());
                    }
                    sysUserRole.setUserId(user.getUserId());
                    userRoleList.add(sysUserRole);


                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
                } else if (isUpdateSupport) {
                    user.setUpdateBy(operName);
                    this.updateUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        //根据导入类型设置角色
        userRoleMapper.batchUserRole(userRoleList);

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 导入学生数据
     *
     * @param importStudentList 用户数据列表
     * @param updateSupport     是否更新支持，如果已存在，则进行更新数据
     * @param operName          操作用户
     * @return 结果
     */
    @Override
    @Transactional
    public String importStudent(List<ImportStudentVo> importStudentList, boolean updateSupport, String operName) {

        Long[] studentRoleIds = roleMapper.selectRoleAll().stream().filter(item -> item.getRoleKey().equals("student")).map(item -> item.getRoleId()).toArray(Long[]::new);
        Map<String, Long> subjectMap = subjectService.list().stream().collect(Collectors.toMap(Subject::getMapKeyName, Subject::getId));
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ImportStudentVo importStudentVo : importStudentList) {
            try {
                if (StringUtils.isEmpty(importStudentVo.getXm())){
                    continue;
                }
                //存储用户信息
                SysUser sysUser = new SysUser();
                Student student = new Student();
                StudentDetail studentDetail = new StudentDetail();
                sysUser.setUserType(UserTypeConstants.ROLE_STUDENT);
                sysUser.setUserName("s" + importStudentVo.getZjhm());
                sysUser.setNickName(importStudentVo.getXm());
                sysUser.setCreateBy(SecurityUtils.getUsername());
                sysUser.setPassword(SecurityUtils.encryptPassword(importStudentVo.getZjhm()));
                sysUser.setRoleIds(studentRoleIds);
                sysUser.setEmail(importStudentVo.getYx()!=null?importStudentVo.getYx():"");
                sysUser.setPhonenumber(importStudentVo.getYddh()!=null?importStudentVo.getYddh():"");

                sysUser.setRemark("学生");
                if(importStudentVo.getXb()!=null){
                    sysUser.setSex(importStudentVo.getXb().equals("女") ? "0" : "1");
                }
                insertUser(sysUser);


                //存储学生信息
                student.setUserId(sysUser.getUserId());

                student.setStuName(importStudentVo.getXm());

                if (StringUtils.isNotEmpty(importStudentVo.getXb())){
                    student.setStuGender(importStudentVo.getXb().equals("女") ? 0 : 1);
                }

                if (StringUtils.isNotEmpty(importStudentVo.getZjhm())){
                    student.setStuCardId(importStudentVo.getZjhm());
                }

                if (StringUtils.isNotEmpty(importStudentVo.getKsbh())){
                    student.setStuExamId(importStudentVo.getKsbh());
                }
                if (StringUtils.isNotEmpty(importStudentVo.getByny())){
                    student.setStuGraduateDate(importStudentVo.getByny().substring(0,4));
                }

                if (StringUtils.isNotEmpty(importStudentVo.getCsrq())){
                    String birth = new StringBuilder(importStudentVo.getCsrq()).insert(4, "-").insert(7, "-").toString();
                    student.setStuBirth(birth);
                }
                Long subId = null;
                if (importStudentVo.getTjzyfx()!=null){
                    subId = subjectMap.get(importStudentVo.getTjzy()+":"+importStudentVo.getTjzyfx());
                    if(subId==null){
                        throw new CustomException("数据库未存储过"+importStudentVo.getTjzy()+":"+importStudentVo.getTjzyfx()+"学科信息,请先到学科管理导入相关信息");
                    }
                }

                student.setSubId(subId);
                studentService.save(student);

                //存储学生详细信息
                Long stuId = student.getId();

                studentDetail.setStuId(stuId);

                if (importStudentVo.getYzybkxx()!=null){
                    //一志愿报考学校
                    studentDetail.setStuPgFirstWishSchool(importStudentVo.getYzybkxx());
                }


                if (importStudentVo.getYzybkzy()!=null){
                    //一志愿报考专业
                    studentDetail.setStuPgFirstWishMajor(importStudentVo.getYzybkzy());
                }

                if (importStudentVo.getYzybkzyfx()!=null){
                    //一志愿报考专业方向
                    studentDetail.setStuPgFirstWishDirection(importStudentVo.getYzybkzyfx());
                }

                if (importStudentVo.getByny()!=null){
                    //毕业时间
                    if (importStudentVo.getByny().length()<7){
                        studentDetail.setStuUgTime(importStudentVo.getByny());
                    }else {
                        studentDetail.setStuUgTime(importStudentVo.getByny());
                    }
                }

               if (importStudentVo.getBydw()!=null){
                   //毕业单位名称
                   studentDetail.setStuUgSchool(importStudentVo.getBydw());
               }

               if (importStudentVo.getByzy()!=null){
                   //毕业专业名称
                   studentDetail.setStuUgMajor(importStudentVo.getByzy());
               }

                //手机号
                if(importStudentVo.getYddh()!=null){
                    studentDetail.setStuMobile(importStudentVo.getYddh());
                }

                //邮箱
                if(importStudentVo.getYx()!=null){
                    studentDetail.setStuEmail(importStudentVo.getYx());
                }

                //奖励处罚
                if (importStudentVo.getJlcf()!=null){
                    studentDetail.setStuReward(importStudentVo.getJlcf());
                }

                if (StringUtils.isNotEmpty(importStudentVo.getWgy())){
                    //外国语分数
                    studentDetail.setStuPgExamForeignScore(new BigDecimal(importStudentVo.getWgy()));
                }

                if (StringUtils.isNotEmpty(importStudentVo.getZzll())){
                    //政治分数
                    studentDetail.setStuPgExamPolitical(new BigDecimal(importStudentVo.getZzll()));
                }

                if (StringUtils.isNotEmpty(importStudentVo.getSx())){
                    //数学
                    studentDetail.setStuPgExamMath(new BigDecimal(importStudentVo.getSx()));
                }

                if (StringUtils.isNotEmpty(importStudentVo.getZmt())){
                    //自命题2
                    studentDetail.setStuPgExamSchool(new BigDecimal(importStudentVo.getZmt()));
                }

                if (StringUtils.isNotEmpty(importStudentVo.getCszf())){
                    //总分
                    studentDetail.setStuPgExamTotal(new BigDecimal(importStudentVo.getCszf()));
                }

               if (StringUtils.isNotEmpty(importStudentVo.getFscj())){
                   //复试成绩
                   studentDetail.setStuPgReview(new BigDecimal(importStudentVo.getFscj()));
               }

               if (StringUtils.isNotEmpty(importStudentVo.getZcj())){
                   //总分
                   studentDetail.setStuPgTotal(new BigDecimal(importStudentVo.getZcj()));
               }

               if (importStudentVo.getTxdz()!=null){
                   //通讯地址
                   studentDetail.setStuResidenceAddress(importStudentVo.getTxdz());
               }

               //考生类别
               if (importStudentVo.getKslylb()!=null){
                   studentDetail.setStuSourceType(importStudentVo.getKslylb());
               }

               if (importStudentVo.getBz()!=null){
                   studentDetail.setStuComment(importStudentVo.getBz());
               }

               if (StringUtils.isNotEmpty(importStudentVo.getTjzy())){
                   studentDetail.setStuPgSecondMajor(importStudentVo.getTjzy());
               }

                if (StringUtils.isNotEmpty(importStudentVo.getTjzyfx())){
                    studentDetail.setStuPgSecondDirection(importStudentVo.getTjzyfx());
                }

                //保存学生详细信息
                studentDetailService.save(studentDetail);


                //保存学生家庭成员信息
                List<StuMemberInfo> stuMemberInfos = new ArrayList<>();
                for (String memberStr : importStudentVo.getJtcy().split("#")) {
                    if (memberStr.length() < 5) {
                        continue;
                    }
                    StuMemberInfo stuMemberInfo = new StuMemberInfo();
                    String[] split = memberStr.split("\\|");
                    int length = split.length;
                    stuMemberInfo.setStuId(student.getId());
                    stuMemberInfo.setName(length>0?split[0]:"");
                    stuMemberInfo.setRelation(length>1?split[1]:"");
                    stuMemberInfo.setProfession(length>2?split[2]:"");
                    stuMemberInfo.setPhone(length>3?split[3]:"");
                    stuMemberInfos.add(stuMemberInfo);
                }
                memberInfoService.saveBatch(stuMemberInfos);

                //存储第一次登录信息
                UpdatePassword updatePassword = new UpdatePassword();
                updatePassword.setUserId(sysUser.getUserId());
                updatePassword.setIsUpdated(0);
                updatePasswordService.save(updatePassword);


                successNum++;
//                successMsg.append("<br/>" + successNum + "、用户 " + importStudentVo.getXm() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、用户 " + importStudentVo.getXm() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
                throw new CustomException(failureMsg.toString());
            }

        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 导入导师数据
     * @param importInstructorList  用户数据列表
     * @param updateSupport         是否更新支持，如果已存在，则进行更新数据
     * @param operName              操作用户
     * @return
     */
    @Transactional
    @Override
    public String importInstructor(List<ImportInstructorVo> importInstructorList, boolean updateSupport, String operName) {
        Long[] instructorRoleIds = roleMapper.selectRoleAll().stream().filter(item -> item.getRoleKey().equals("instructor")).map(item -> item.getRoleId()).toArray(Long[]::new);
        int successNum = 0;
        StringBuilder successMsg = new StringBuilder();

        for (ImportInstructorVo importInstructorVo : importInstructorList) {

            if (StringUtils.isEmpty(importInstructorVo.getInsCartId())){
                break;
            }
            try {
                //存储用户信息
                SysUser sysUser = new SysUser();
                Instructor instructor = new Instructor();
                sysUser.setUserName("t" + importInstructorVo.getInsCartId());
                sysUser.setNickName(importInstructorVo.getInsName());

                sysUser.setCreateBy(SecurityUtils.getUsername());
                if (importInstructorVo.getInsGender()!=null){
                    sysUser.setSex(importInstructorVo.getInsGender().equals("男")?"1":"0");
                }
                if (importInstructorVo.getInsMobile()!=null){
                    sysUser.setPhonenumber(importInstructorVo.getInsMobile());
                }
                if (importInstructorVo.getInsEmail()!=null){
                    sysUser.setEmail(importInstructorVo.getInsEmail());
                }
                sysUser.setPassword(SecurityUtils.encryptPassword(importInstructorVo.getInsCartId()));
                sysUser.setRoleIds(instructorRoleIds);
                sysUser.setUserType(2);
                sysUser.setRemark("导师");
                insertUser(sysUser);

                //存储导师信息
                instructor.setUserId(sysUser.getUserId());
                instructor.setInsName(importInstructorVo.getInsName());
                instructor.setInsCartId(importInstructorVo.getInsCartId());

                if (importInstructorVo.getInsGender()!=null){
                    instructor.setInsGender(importInstructorVo.getInsGender().equals("男")?1l:0);
                }

                if (importInstructorVo.getInsPicture()!=null){
                    instructor.setInsPicture(importInstructorVo.getInsPicture());
                }


                if (importInstructorVo.getInsProfessor()!=null){
                    instructor.setInsProfessor(importInstructorVo.getInsProfessor());
                }

                if (importInstructorVo.getInsCommander()!=null){
                    instructor.setInsCommander(importInstructorVo.getInsCommander());
                }

                if (importInstructorVo.getInsMajor()!=null){
                    instructor.setInsMajor(importInstructorVo.getInsMajor());
                }

                if (importInstructorVo.getInsDirection()!=null){
                    instructor.setInsDirection(importInstructorVo.getInsDirection());
                }

                if (importInstructorVo.getInsBirth()!=null){
                    instructor.setInsBirth(importInstructorVo.getInsBirth());
                }

                if (importInstructorVo.getInsNativePlace()!=null){
                    instructor.setInsNativePlace(importInstructorVo.getInsNativePlace());
                }

                if (importInstructorVo.getInsPoliticalOutlook()!=null){
                    instructor.setInsPoliticalOutlook(importInstructorVo.getInsPoliticalOutlook());
                }

                if (importInstructorVo.getInsEducation()!=null){
                    instructor.setInsEducation(importInstructorVo.getInsEducation());
                }

                if (importInstructorVo.getInsAcademicDegree()!=null){
                    instructor.setInsAcademicDegree(importInstructorVo.getInsAcademicDegree());
                }

                if (importInstructorVo.getInsMobile()!=null){
                    instructor.setInsMobile(importInstructorVo.getInsMobile());
                }

                if (importInstructorVo.getInsPhone()!=null){
                    instructor.setInsPhone(importInstructorVo.getInsPhone());
                }

                if (importInstructorVo.getInsEmail()!=null){
                    instructor.setInsEmail(importInstructorVo.getInsEmail());
                }

                instructorService.save(instructor);


                //存储导师详细信息

                InsDetail insDetail = new InsDetail();
                insDetail.setInsId(instructor.getId());

                if (importInstructorVo.getInsExperience()!=null){
                    insDetail.setInsExperience(importInstructorVo.getInsExperience());
                }

                if (importInstructorVo.getInsTeachingUndergraduate()!=null){
                    insDetail.setInsTeachingUndergraduate(importInstructorVo.getInsTeachingUndergraduate());
                }

                if (importInstructorVo.getInsTeachingGraduate()!=null){
                    insDetail.setInsTeachingGraduate(importInstructorVo.getInsTeachingGraduate());
                }

//                if (importInstructorVo.getInsAcademicWorks()!=null){
//                    insDetail.setInsAcademicWorks(importInstructorVo.getInsAcademicWorks());
//                }

                if (importInstructorVo.getInsPublishTextbook()!=null){
                    insDetail.setInsPublishTextbook(importInstructorVo.getInsPublishTextbook());
                }

                if (importInstructorVo.getInsTrainingGraduate()!=null){
                    insDetail.setInsTrainingGraduate(importInstructorVo.getInsTrainingGraduate());
                }

//                if (importInstructorVo.getInsTrainingGraduated()!=null){
//                    insDetail.setInsTrainingGraduated(importInstructorVo.getInsTrainingGraduated());
//                }

                if (importInstructorVo.getInsSocialAppointment()!=null){
                    insDetail.setInsSocialAppointment(importInstructorVo.getInsSocialAppointment());
                }
                if (importInstructorVo.getInsReward()!=null){
                    insDetail.setInsReward(importInstructorVo.getInsReward());
                }

                if (importInstructorVo.getInsHonor()!=null){
                    insDetail.setInsHonor(importInstructorVo.getInsHonor());
                }

                if (importInstructorVo.getInsOther()!=null){
                    insDetail.setInsOther(importInstructorVo.getInsOther());
                }

                insDetailService.save(insDetail);

                //存储论文信息
                String papersStr = importInstructorVo.getPapersStr();
                InsPaper insPaper = new InsPaper();
                insPaper.setInsId(instructor.getId());
                insPaper.setDetail(papersStr);
                insPaperService.save(insPaper);

                //存储项目信息
                String projectsStr = importInstructorVo.getProjectsStr();
                InsProject insProject = new InsProject();
                insProject.setInsId(instructor.getId());
                insProject.setDetail(projectsStr);
                insProjectService.save(insProject);



                //存储第一次登录信息
                UpdatePassword updatePassword = new UpdatePassword();
                updatePassword.setUserId(sysUser.getUserId());
                updatePassword.setIsUpdated(0);
                updatePasswordService.save(updatePassword);

                successNum++;

            }catch (Exception e){
                throw new CustomException("导师:"+importInstructorVo.getInsName()+"导入失败,请联系管理员");
            }

        }
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");

        return successMsg.toString();
    }
}
